package com.timkj.zgyd.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.timkj.zgyd.dao.ZgydTelephoneDao;
import com.timkj.zgyd.dao.ZgydUserDao;
import com.timkj.zgyd.dto.GetTelephoneListDto;
import com.timkj.zgyd.entity.ResultVO;
import com.timkj.zgyd.entity.Zgyd_User_tb;
import com.timkj.zgyd.entity.Zgyd_telephone_tb;
import com.timkj.zgyd.service.ZgydUserService;
import com.timkj.zgyd.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;

/**
 * @author : liji
 * @date : 2020-09-01 15:14
 */
@Slf4j
@Service
public class ZgydUserServiceImpl implements ZgydUserService {
    private ZgydUserDao zgydUserDao;
    private ZgydTelephoneDao zgydTelephoneDao;

    @Autowired
    public ZgydUserServiceImpl(
            ZgydUserDao zgydUserDao,
            ZgydTelephoneDao zgydTelephoneDao) {
        this.zgydUserDao = zgydUserDao;
        this.zgydTelephoneDao = zgydTelephoneDao;
    }

    /**
     * 存入openid
     */
    @Override
    public String saveOpenid(String code,String state) {
        RestTemplate restTemplate = new RestTemplate();
        String appid = "wxc66c054a1c53bc0d";
        String appSecret = "f51bb6c18d6e6c905743af1486cd4b11";
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+appSecret+"&code="+code+"&grant_type=authorization_code";
        JSONObject jsonObject = JSONObject.parseObject(restTemplate.getForEntity(url, String.class).getBody());
        String openid = jsonObject.getString("openid");
        if (openid!=null){
            Zgyd_User_tb zgydUserTb = zgydUserDao.SelectUserByOpenid(openid);
            if (zgydUserTb!=null){
                return openid;
            }else {
                zgydUserDao.InsertUserOpenid(openid);
                return openid;
            }
        }else {
            return null;
        }
    }

    /**
     * 拉取telephone列表
     * @param dto
     */
    @Override
    public ResultVO getTelephoneList(GetTelephoneListDto dto, HttpServletRequest request) {
        ResultVOUtil voUtil = new ResultVOUtil();
        if (dto.getOpenid().equals("null")){
            dto.setOpenid(String.valueOf(request.getSession().getAttribute("openid")));
        }
        if (zgydUserDao.SelectUserByOpenid(dto.getOpenid())==null){
            return voUtil.unkownError("zgyd_user_tb中没有这个openid : " + dto.getOpenid());
        } else {
            ArrayList<Zgyd_telephone_tb> telephoneTbs = zgydTelephoneDao.SelectTelephoneByStatus("可选");
            if (dto.getList()<=telephoneTbs.size()){
                ArrayList<Zgyd_telephone_tb> telephoneTbs1 = new ArrayList<>();
                HashSet<Integer> telephoneList = new HashSet<>();
                while (telephoneList.size()<dto.getList()){
                    telephoneList.add(new Random().nextInt(telephoneTbs.size()) + 1);
                }
                for (int telephoneListIndex : telephoneList){
                    telephoneTbs1.add(telephoneTbs.get(telephoneListIndex-1));
                }
                return voUtil.success(telephoneTbs1);
            }else {
                return voUtil.success(telephoneTbs);
            }
        }
    }

    /**
     * 模糊查询手机号
     */
    @Override
    public ResultVO searchTelephoneNumber(String openid, String number,HttpServletRequest request) {
        ResultVOUtil voUtil = new ResultVOUtil();
        if (openid.equals("null")){
            openid = (String.valueOf(request.getSession().getAttribute("openid")));
        }
        if (zgydUserDao.SelectUserByOpenid(openid)==null){
            return voUtil.unkownError("zgyd_user_tb中没有这个openid : " + openid);
        }else {
            ArrayList<Zgyd_telephone_tb> telephone_tbs = zgydUserDao.searchTelephoneNumber(number);
            if (telephone_tbs.size()<=10){
                return voUtil.success(telephone_tbs);
            }else {
                ArrayList<Zgyd_telephone_tb> telephone_tbs1 = new ArrayList<>();
                HashSet<Integer> telephoneList = new HashSet<>();
                while (telephoneList.size()<10){
                    telephoneList.add(new Random().nextInt(telephone_tbs.size()) + 1);
                }
                for (int telephoneListIndex : telephoneList){
                    telephone_tbs1.add(telephone_tbs.get(telephoneListIndex-1));
                }
                return voUtil.success(telephone_tbs1);
            }
        }
    }
}
